package com.first.nell.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.first.nell.R
import com.first.nell.base.BaseDialogAction
import com.first.nell.databinding.DialogFirstNellCityPickerBinding

class NgnCityPickerDialog : DialogFragment(), BaseDialogAction {

    companion object {
        fun generateFirstNellDialog(title: String?): NgnCityPickerDialog {
            val dialog = NgnCityPickerDialog()
            dialog.mFirstNellTitle = title
            return dialog
        }
    }

    private lateinit var mFirstNellViewFirstNellBinding: DialogFirstNellCityPickerBinding
    private lateinit var mPopListAdapter: PopListAdapter

    var mFirstNellState: String = ""
    var mFirstNellCity: String = ""
    var mFirstNellArea: String = ""
    var mFirstNellPickFirstNellType: Int = R.integer.PickerTypeState
    var mFirstNellUIFirstNellTag: String = ""

    private var mFirstNellOnFirstNellActionFirstNellListener: (String, Int, PopListItem, Int) -> Unit =
        { uTag, pickTpe, listItem, index -> }
    private val mFirstNellDataFirstNellList = mutableListOf<PopListItem>() as ArrayList<PopListItem>
    private var mFirstNellTitle: String? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mFirstNellViewFirstNellBinding = DialogFirstNellCityPickerBinding.inflate(layoutInflater)
        return mFirstNellViewFirstNellBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initFirstNellSubviews()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        mFirstNellViewFirstNellBinding.textDialogCityTitle.text = mFirstNellTitle
        mPopListAdapter.notifyDataSetChanged()
    }

    override fun initFirstNellSubviews() {
        mPopListAdapter =
            PopListAdapter(mFirstNellDataFirstNellList)

        mFirstNellViewFirstNellBinding.rcDialogCityList.layoutManager =
            LinearLayoutManager(context)
        mFirstNellViewFirstNellBinding.rcDialogCityList.adapter = mPopListAdapter

        mFirstNellViewFirstNellBinding.textDialogCityPickerState.text = mFirstNellState
        mFirstNellViewFirstNellBinding.textDialogCityPickerCity.text = mFirstNellCity
        mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea

        if (mFirstNellState.isBlank()) {
            mFirstNellViewFirstNellBinding.rlDialogCityPickerResultBg.visibility = View.GONE
        } else {
            mFirstNellViewFirstNellBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
        }

        mFirstNellViewFirstNellBinding.textDialogCityPickerState.setOnClickListener {
            mFirstNellPickFirstNellType = R.integer.PickerTypeState
            mFirstNellState = ""
            mFirstNellCity = ""
            mFirstNellArea = ""
            mFirstNellViewFirstNellBinding.textDialogCityPickerState.text = mFirstNellState
            mFirstNellViewFirstNellBinding.textDialogCityPickerCity.text = mFirstNellCity
            mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea
            mFirstNellViewFirstNellBinding.rlDialogCityPickerResultBg.visibility = View.GONE
            mFirstNellOnFirstNellActionFirstNellListener.invoke(
                mFirstNellUIFirstNellTag,
                R.integer.PickerTypeStateReload,
                PopListItem(mFirstNellState, mFirstNellState),
                -1
            )
        }

        mFirstNellViewFirstNellBinding.textDialogCityPickerCity.setOnClickListener {
            mFirstNellPickFirstNellType = R.integer.PickerTypeCity
            mFirstNellArea = ""
            mFirstNellViewFirstNellBinding.textDialogCityPickerState.text = mFirstNellState
            mFirstNellViewFirstNellBinding.textDialogCityPickerCity.text = mFirstNellCity
            mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea
            mFirstNellViewFirstNellBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
            mFirstNellOnFirstNellActionFirstNellListener.invoke(
                mFirstNellUIFirstNellTag,
                R.integer.PickerTypeCityReload,
                PopListItem(mFirstNellCity, mFirstNellCity),
                -1
            )
        }

        mFirstNellViewFirstNellBinding.textDialogCityPickerTown.setOnClickListener {
            mFirstNellPickFirstNellType = R.integer.PickerTypeCity
            mFirstNellViewFirstNellBinding.textDialogCityPickerState.text = mFirstNellState
            mFirstNellViewFirstNellBinding.textDialogCityPickerCity.text = mFirstNellCity
            mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea
            mFirstNellViewFirstNellBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
            mFirstNellOnFirstNellActionFirstNellListener.invoke(
                mFirstNellUIFirstNellTag,
                R.integer.PickerTypeTownReload,
                PopListItem(mFirstNellArea, mFirstNellArea),
                -1
            )
        }

        mPopListAdapter.setFirstNellOnFirstNellItemFirstNellClickFirstNellListener { i, dialogListItem ->
            if (mFirstNellPickFirstNellType == R.integer.PickerTypeState) {
                mFirstNellState = dialogListItem.display
                mFirstNellCity = ""
                mFirstNellArea = ""
                mFirstNellViewFirstNellBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
                mFirstNellViewFirstNellBinding.textDialogCityPickerState.text = mFirstNellState
                mFirstNellViewFirstNellBinding.textDialogCityPickerCity.text = mFirstNellCity
                mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea
                mFirstNellOnFirstNellActionFirstNellListener.invoke(
                    mFirstNellUIFirstNellTag,
                    R.integer.PickerTypeState,
                    dialogListItem,
                    i
                )

            } else if (mFirstNellPickFirstNellType == R.integer.PickerTypeCity) {
                mFirstNellCity = dialogListItem.content
                mFirstNellArea = ""
                mFirstNellViewFirstNellBinding.textDialogCityPickerCity.text = mFirstNellCity
                mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea
                mFirstNellOnFirstNellActionFirstNellListener.invoke(
                    mFirstNellUIFirstNellTag,
                    R.integer.PickerTypeCity,
                    dialogListItem,
                    i
                )

            } else if (mFirstNellPickFirstNellType == R.integer.PickerTypeTown) {
                mFirstNellArea = dialogListItem.content
                mFirstNellViewFirstNellBinding.textDialogCityPickerTown.text = mFirstNellArea
                mFirstNellOnFirstNellActionFirstNellListener.invoke(
                    mFirstNellUIFirstNellTag,
                    R.integer.PickerTypeTown,
                    dialogListItem,
                    i
                )
            }
        }

        mFirstNellViewFirstNellBinding.submitDialogCityPicker.setOnClickListener {
            if (mFirstNellState.isBlank() || mFirstNellCity.isBlank() || mFirstNellArea.isBlank()) {
                Toast.makeText(context, "Please slect $mFirstNellTitle", Toast.LENGTH_LONG).show()
                return@setOnClickListener
            }
            mFirstNellOnFirstNellActionFirstNellListener.invoke(
                mFirstNellUIFirstNellTag,
                R.integer.PickerTypeConfirm,
                PopListItem("", ""),
                -1
            )
            dismiss()
        }
    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {

    }

    override fun showFirstNellWithFirstNellUIFirstNellTag(gTag: String, fragmentManager: FragmentManager) {
        mFirstNellUIFirstNellTag = gTag
        showNow(fragmentManager, NgnCityPickerDialog::class.java.simpleName)
    }

    fun getFirstNellState(): String {
        return mFirstNellState
    }

    fun getFirstNellCity(): String {
        return mFirstNellCity
    }

    fun getFirstNellTown(): String {
        return mFirstNellArea
    }

    fun setFirstNellOnFirstNellDataFirstNellClickFirstNellListener(listener: (String, Int, PopListItem, Int) -> Unit) {
        mFirstNellOnFirstNellActionFirstNellListener = listener
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setFirstNellDataFirstNellList(pickType: Int, itemList: List<PopListItem>) {
        mFirstNellPickFirstNellType = pickType
        mFirstNellDataFirstNellList.clear()
        mFirstNellDataFirstNellList.addAll(itemList)

        if (isVisible) {
            mPopListAdapter.notifyDataSetChanged()
        }
    }
}